package com.smartwebx.ha.web.gis.module.screen;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.smartwebx.ha.biz.assign.HaAreaManager;
import com.smartwebx.ha.dal.assign.bean.HaArea;
import com.smartwebx.ha.dal.util.MyFunctions;
import com.smartwebx.ha.web.admin.common.HaUserSys;

public class AjaxUserAreaInfo extends BasicScreen {
	@Autowired
	private HttpServletResponse response;
	@Autowired
	private HaAreaManager haAreaManager;
	@Autowired
	private HttpSession session;

	public Object info() throws Exception {
		HaUserSys user = (HaUserSys) session.getAttribute("haUser");
		HaArea area = haAreaManager.findOne(user.getAreaId());
		GisUserInfo gisUserInfo = new GisUserInfo();
		gisUserInfo.setId(area.getId());
		gisUserInfo.setName(area.getName());
		gisUserInfo.setCode(area.getCode());
		Iterable<Double> centerPoint = Iterables.transform(Splitter.on(',')
				.split(area.getCenterPoint()), MyFunctions
				.parseNumberFunctionFor(Double.class));
		gisUserInfo.setCenterPoint(Iterables.toArray(centerPoint, Double.class));
		Iterable<Double> areaMaxMinXy = Iterables.transform(Splitter.on(',')
				.split(area.getMaxMinPoint()), MyFunctions
				.parseNumberFunctionFor(Double.class));
		if (!Iterables.isEmpty(areaMaxMinXy)) {
			GisExtent spatialReference = new GisExtent();
			spatialReference.setXmin(Iterables.get(areaMaxMinXy, 0));
			spatialReference.setYmin(Iterables.get(areaMaxMinXy, 1));
			spatialReference.setXmax(Iterables.get(areaMaxMinXy, 2));
			spatialReference.setYmax(Iterables.get(areaMaxMinXy, 3));

			gisUserInfo.setExtent(spatialReference);
		}
		gisUserInfo.setPoints(area.getPoints());
		gisUserInfo.setZoom(area.getZoomLevel());

		return gisUserInfo;
	}

	class GisUserInfo {
		private int id;
		private String name;
		private String code;
		private Double[] centerPoint;
		private GisExtent extent;
		private String points;
		private int zoom;
		
		public void setZoom(int zoom){
			this.zoom = zoom;
		}	
		
		public int getZoom(){
			return this.zoom;
		}
		
		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getCode() {
			return code;
		}

		public void setCode(String code) {
			this.code = code;
		}

		public Double[] getCenterPoint() {
			return centerPoint;
		}

		public void setCenterPoint(Double[] centerPoint) {
			this.centerPoint = centerPoint;
		}

		public GisExtent getExtent() {
			return extent;
		}

		public void setExtent(GisExtent extent) {
			this.extent = extent;
		}
		
		public String getPoints() {
			return points;
		}

		public void setPoints(String points) {
			this.points = points;
		}
	}

	class GisExtent {
		private double xmin;
		private double ymin;
		private double xmax;
		private double ymax;
		private Map<String, Integer> spatialReference = ImmutableMap.of("wkid",
				4326);

		public double getXmin() {
			return xmin;
		}

		public void setXmin(double xmin) {
			this.xmin = xmin;
		}

		public double getYmin() {
			return ymin;
		}

		public void setYmin(double ymin) {
			this.ymin = ymin;
		}

		public double getXmax() {
			return xmax;
		}

		public void setXmax(double xmax) {
			this.xmax = xmax;
		}

		public double getYmax() {
			return ymax;
		}

		public void setYmax(double ymax) {
			this.ymax = ymax;
		}

		public Map<String, Integer> getSpatialReference() {
			return spatialReference;
		}

		public void setSpatialReference(Map<String, Integer> spatialReference) {
			this.spatialReference = spatialReference;
		}
	}

}
